#!/bin/bash

echo "This script will help you set up SSH passwordless login to target hosts."
echo ""

# 提示用户输入目标主机列表，以空格分隔
read -p "Enter target hosts (separated by spaces): " hosts

# 提示用户输入SSH用户名
read -p "Enter SSH username: " username

# 生成SSH密钥对
ssh-keygen -t rsa

# 将公钥分发到所有目标主机
for host in $hosts; do
    echo "Copying public key to $host..."
    ssh-copy-id -i ~/.ssh/id_rsa.pub $username@$host
done

# 测试SSH连接
for host in $hosts; do
    echo "Testing SSH connection to $host..."
    ssh -o BatchMode=yes $username@$host exit
    if [ $? -eq 0 ]; then
        echo "SSH connection to $host successful."
    else
        echo "SSH connection to $host failed. Please check your configuration."
    fi
done

echo "Done."
